Ext.define('Admin.view.processdefinition.ProcessDefinitionController', {
    extend: 'Ext.app.ViewController',//重点1:继承视图控制器

    alias: 'controller.processdefinition',//重点2:视图控制器的别名,注入到视图组件的controller属性中.
	onClickProcessDefinitionGridUploadButton: function () {
		Ext.create(Ext.apply({xtype: 'processdefinition-upload-window'}));
    },
    onClickProcessDefinitionUploadFormCancelButton: function (btn) {
		var win = btn.up('window');
        if (win) {
            win.close();
        }
    },
	onClickProcessDefinitionUploadFormUploadButton: function (btn) {
		var form = btn.up('form');
		form.getForm().submit({       
			url:'processdefinition/deploy',
			waitMsg: '正在上传，请耐心等待....',
			success: function(form, action){    
				Ext.Msg.alert('Success', action.result.msg,function(){
					btn.up('window').close();
					//form.getViewModel().getStore('processDefinitionGridStore').reload();
                    Ext.getCmp('process-definition-grid').getStore().reload();
					
				});       
			},       
			failure: function(form, action){
				Ext.Msg.alert('Error', action.result.msg);
			}
		});
    },

    onClickProcessDefinitionReadResourceButton: function (view, recIndex, cellIndex, item, e, record) {
    	var url = 'processdefinition/resource?pdid='+record.get('id')+'&resourceName='+record.get('resourceName');
		//window.open(url);
		var win = new Ext.window.Window({
			title : '查看流程文件bpmn',
			width : 800,
			height : 500,
			layout:'fit',
			items:[{
				 xtype:'panel'
				,id:'read-resource-panel'
				,autoScroll: true
				//,html:''
			}]
		});
		win.show();
		Ext.Ajax.request({
	        url:url,
	        success: function(response,options){
				var panel =Ext.getCmp("read-resource-panel");
				panel.body.update("<xmp>"+response.responseText+"</xmp>");//autoScroll: true
				//panel.body.update("<textarea style='width:775px;height:420px;overflow:auto;'>"+response.responseText+"</textarea>");
	        }
		});
    },
    onClickProcessDefinitionReadDiagramResourceButton: function(view, recIndex, cellIndex, item, e, record) {
        var url = 'processdefinition/resource?pdid=' + record.get('id') + '&resourceName=' + record.get('diagramResourceName');
        var win = new Ext.window.Window({
            title: '查看流程PNG',
            width : 860,
			height : 500,
            layout: 'fit',
            items:[ new Ext.Panel({         
	           resizeTabs :true,
	           autoScroll : true,
	           html:'<iframe scrolling="auto" frameborder="0" width="100%" height="100%" src='+url+'></iframe>'
	       })]
        });
        win.show();
    },
	onClickProcessDefinitionGridDeleteButton: function (view, recIndex, cellIndex, item, e, record) {
		Ext.Ajax.request({ 
			url : 'processdefinition/delete', 
			method : 'post', 
			params : { 
				deploymentId :record.get('deploymentId')//record.data.deploymentId
			}, 
			success: function(response, options) {
                var json = Ext.util.JSON.decode(response.responseText);
	            if(json.success){
	            	Ext.Msg.alert('系统提示', json.msg, function() {
	                    view.getStore().reload();
	                });
		        }else{
		        	 Ext.Msg.alert('系统提示', json.msg);
		        }
            }
		});
			
    },
    onClickProcessDefinitionActiveButton: function(view, recIndex, cellIndex, item, e, record) {
        var url = 'processdefinition/update/active/' + record.get('id');
         Ext.Ajax.request({
            url: url,
            success: function(response, options) {
                var json = Ext.util.JSON.decode(response.responseText);
                if (json.success) {
                    Ext.Msg.alert('系统提示', json.msg, function() {
                        view.getStore().reload();
                    });
                } else {
                    Ext.Msg.alert('系统提示', json.msg);
                }
            }
        });
       
    },	
    onClickProcessDefinitionSuspendButton: function(view, recIndex, cellIndex, item, e, record) {
       var url = 'processdefinition/update/suspend/' + record.get('id');
       Ext.Ajax.request({
            url: url,
            success: function(response, options) {
                var json = Ext.util.JSON.decode(response.responseText);
                if (json.success) {
                    Ext.Msg.alert('系统提示', json.msg, function() {
                        view.getStore().reload();
                    });
                } else {
                    Ext.Msg.alert('系统提示', json.msg);
                }
            }
        });
    },
    onClickProcessDefinitionGridConvertModelButton: function(view, recIndex, cellIndex, item, e, record) {
       var url = 'processdefinition/convert-to-model/'+record.get('id');
       Ext.Ajax.request({
            url: url,
            success: function(response, options) {
                var json = Ext.util.JSON.decode(response.responseText);
                if (json.success) {
                    Ext.Msg.alert('系统提示', json.msg, function() {
                        view.getStore().reload();
                    });
                } else {
                    Ext.Msg.alert('系统提示', json.msg);
                }
            }
        });
    }
});
